﻿using System;
using System.Linq;
using System.Windows.Forms;

namespace GDM.Hogar.Controles.ControlesPersonalizados.Grilla
{
    public partial class PagedFlexGrid : UserControl
    {
        public Action OnCreate;
        public Action<object> OnDelete;
        public Action<object> OnEdit;

        public PagedFlexGrid()
        {
            InitializeComponent();
            lblPageInfo.Enabled = false;
            grid.OnPageChange = () =>
                {
                    var totalRecords = grid.TotalRecords();
                    tslTotal.Text = totalRecords.ToString();
                    var totalPages = grid.TotalPages();
                    lblPageInfo.Text = string.Format("Pagina {0} de {1}", grid.CurrentPage + 1, totalPages);
                    btnFirst.Enabled = grid.CurrentPage > 0;
                    btnPrevious.Enabled = grid.CurrentPage > 0;
                    btnNext.Enabled = grid.CurrentPage < totalPages - 1;
                    btnLast.Enabled = grid.CurrentPage < totalPages - 1;
                    if (!UserCanDelete && !UserCanEdit) return;

                    SetActionsBarStatus(totalRecords);
                };

            btnFirst.Click += delegate
            {
                grid.OnGotoFirst();
            };

            btnPrevious.Click += delegate
            {
                grid.OnGotoPrev();
            };

            btnNext.Click += delegate
            {
                grid.OnGotoNext();
            };

            btnLast.Click += delegate
            {
                grid.OnGotoLast();
            };
        }

        public bool UserCanDelete { get; set; }

        public bool UserCanEdit { get; set; }

        public bool UserCanAdd { get; set; }

        public bool ReadOnlyMode { get; set; }

        #region Public Methods

        public void Configure<T>(IQueryable<T> query, int pageSize)
        {
            grid.Configure(query, pageSize);
        }

        public void HidePager()
        {
            tsPager.Visible = false;
        }

        public void Search<T>(IQueryable<T> source, Func<T, bool> criteria)
        {
            grid.Search(source, criteria);
        }

        public void DisableActions(bool disable = true)
        {
            pnlAcciones.Enabled = !disable;
        }

        #endregion Public Methods

        #region Private Methods

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            if (OnCreate != null)
                OnCreate();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            if (grid.SelectedRows.Count <= 0) return;

            var item = grid.SelectedRows[0].DataBoundItem;
            if (OnEdit != null)
                OnEdit(item);
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (grid.SelectedRows.Count <= 0) return;

            var item = grid.SelectedRows[0].DataBoundItem;
            if (OnDelete != null)
                OnDelete(item);
        }

        private void SetActionsBarStatus(int totalRecords)
        {
            btnEditar.Text = ReadOnlyMode ? "Ver" : "Editar";
            btnEditar.Enabled = UserCanEdit && totalRecords > 0;
            btnEliminar.Enabled = UserCanDelete && totalRecords > 0;
            btnNuevo.Enabled = UserCanAdd;
        }

        #endregion Private Methods

        private void grid_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            btnEditar_Click(null, null);
        }
    }
}